package com.symbio.ishenapi.background.controller;

import com.symbio.ishenapi.background.model.Cities;
import com.symbio.ishenapi.background.model.Districts;
import com.symbio.ishenapi.background.model.Provinces;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RequestMapping("provincesController")
public class ProvincesController extends BaseController {

    /**
     * @Author jianghy
     * @Description 省市区三级联动
     * @date
     * @return
     */
    public void findRegionLink() {
        List prosList = new ArrayList();
        //全部省
        List<Provinces> proList = Provinces.dao.getProvinces();
        //全部市
        List<Cities> cityList = Cities.dao.getCities();
        //全部区
        List<Districts> districtsList = Districts.dao.getDistricts();
        for(int i=0;i<proList.size();i++){
            Map map = new LinkedHashMap();
            Provinces pro = proList.get(i);
            map.put("name",pro.getName());
            List citiesList  = new ArrayList();
            for(int j = 0;j<cityList.size();j++){
                Cities city = cityList.get(j);
                if(pro.getCode().equals(city.getProvinceCode())){
                    Map cityMap = new LinkedHashMap();
                    cityMap.put("name",city.getName());
                    //city.getName();
                    List disList = new ArrayList();
                    for(int k = 0;k<districtsList.size();k++){
                        Districts dis = districtsList.get(k);
                        if(city.getCode().equals(dis.getCityCode())){
                            //dis.getName();
                            disList.add(dis.getName());
                        }
                    }
                    cityMap.put("area",disList);
                    citiesList.add(cityMap);
                    map.put("city",citiesList);
                }
            }
            if(citiesList==null||citiesList.size()==0){
                map.put("city",citiesList);
            }
            prosList.add(map);
        }
        renderJsonRet(prosList);
    }

    /**
     * @Author jianghy
     * @Description 查询所有的省
     * @date
     * @return
     */
    public void queryAllProvinces(){
        List mList = new ArrayList();
        //全部省
        List<Provinces> proList = Provinces.dao.getProvinces();
        Map map1 = new HashMap();
        map1.put("code","0");
        map1.put("value","请选择");
        mList.add(map1);
        for(int i=0;i<proList.size();i++){
            Map map = new HashMap();
            Provinces pro = proList.get(i);
            map.put("code",pro.getCode());
            map.put("value",pro.getName());
            mList.add(map);
        }
        renderJsonRet(mList);
    }

    /**
     * @Author jianghy
     * @Description 根据省code查询市
     * @date
     * @return
     */
    public void getCitiesById(){
        List mList = new ArrayList();
        String provinceCode = getPara("provinceCode");
        if("0".equals(provinceCode)||provinceCode==null){
            Map map = new HashMap();
            map.put("code","1");
            map.put("value","请选择");
            mList.add(map);
        }else{
            List<Cities> clist = Cities.dao.getCitiesByProvinceCode(provinceCode);
            Map map1 = new HashMap();
            map1.put("code","1");
            map1.put("value","请选择");
            mList.add(map1);
            for(int i=0;i<clist.size();i++){
                Map map = new HashMap();
                Cities cy = clist.get(i);
                map.put("code",cy.getCode());
                map.put("value",cy.getName());
                mList.add(map);
            }
        }
        renderJsonRet(mList);
    }
    /**
     * @Author jianghy
     * @Description 根据cityCode查询所属县/区
     * @date
     * @return
     */
    public void getDistrictsByCityCode(){
        List mList = new ArrayList();
        String cityCode = getPara("cityCode");
        if("0".equals(cityCode)||cityCode==null){
            Map map = new HashMap();
            map.put("code","1");
            map.put("value","请选择");
            mList.add(map);
        }else{
            List<Cities> clist = Cities.dao.getDistrictsByCityCode(cityCode);
            Map map1 = new HashMap();
            map1.put("code","1");
            map1.put("value","请选择");
            mList.add(map1);
            for(int i=0;i<clist.size();i++){
                Map map = new HashMap();
                Cities cy = clist.get(i);
                map.put("code",cy.getCode());
                map.put("value",cy.getName());
                mList.add(map);
            }
        }
        renderJsonRet(mList);
    }

}
